﻿using DevExpress.AspNetCore.Reporting.QueryBuilder;
using DevExpress.AspNetCore.Reporting.QueryBuilder.Native.Services;
using DevExpress.AspNetCore.Reporting.ReportDesigner;
using DevExpress.AspNetCore.Reporting.ReportDesigner.Native.Services;
using DevExpress.AspNetCore.Reporting.WebDocumentViewer;
using DevExpress.AspNetCore.Reporting.WebDocumentViewer.Native.Services;
using DevExpress.XtraReports;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Web.ReportDesigner;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ReportDesignerServer.Common;
using ReportDesignerServer.Services;

namespace ReportDesignerServer.Controllers
{
    /// <summary>
    /// CustomWebDocumentViewerController
    /// </summary>
    [ApiExplorerSettings(IgnoreApi = true)]
    public class CustomWebDocumentViewerController : WebDocumentViewerController
    {
        public CustomWebDocumentViewerController(IWebDocumentViewerMvcControllerService controllerService) : base(controllerService)
        {
        }
    }

    /// <summary>
    /// CustomReportDesignerController
    /// </summary>
    [ApiExplorerSettings(IgnoreApi = true)]
    public class CustomReportDesignerController : ReportDesignerController
    {
        public CustomReportDesignerController(IReportDesignerMvcControllerService controllerService) : base(controllerService)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reportUrl"></param>
        /// <param name="modelGenerator"></param>
        /// <param name="service"></param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public IActionResult GetDesignerModel([FromForm] string reportUrl, [FromServices] IReportDesignerClientSideModelGenerator modelGenerator, [FromServices] DataSourceService service)
        {
            if (string.IsNullOrEmpty(reportUrl))
            {
                NLogHelper.GetLog().Info("url为空！");
                return BadRequest();
            }

            var dataSources = new Dictionary<string, object>();
            var paras = reportUrl.Split('|');
            var fileName = paras[0];
            var args = paras.Skip(1).ToArray();
        
            var dtSource = service.GetJsonDataSource(fileName, args);
          
            dataSources.Add("json", dtSource);
           
            ReportDesignerModel model = new ReportDesignerModel();
            //linux  XtraReport 构造函数报错
            model = modelGenerator.GetModel(reportUrl, dataSources, ReportDesignerController.DefaultUri, WebDocumentViewerController.DefaultUri, QueryBuilderController.DefaultUri);

            var modeljson = JsonConvert.SerializeObject(model);
            NLogHelper.GetLog().Info("model:" + modeljson);
            var result = DesignerModel(model);
            return result;
        }
    }

    /// <summary>
    /// CustomQueryBuilderController
    /// </summary>
    [ApiExplorerSettings(IgnoreApi = true)]
    public class CustomQueryBuilderController : QueryBuilderController
    {
        public CustomQueryBuilderController(IQueryBuilderMvcControllerService controllerService) : base(controllerService)
        {
        }
    }
}
